<template>
	<view class="container">
		<view class="tab-list">
			<u-tabs :current="current" :list="previewTypeList" :itemStyle="{width: '50vw', padding: '30rpx 0'}"
				:activeStyle="{color:'#FF7300'}" :inactiveStyle="{fontSize: '28rpx', color: '#8F8F8F'}" lineWidth="70rpx"
				lineHeight="4rpx" lineColor="#ff7300" :duration="200" @click="changeCurrent"></u-tabs>
		</view>
		<swiper class="content" :current="current" easing-function="easeOutCubic" :duration="200" @change="changeCurrent">
			<swiper-item v-for="(item, index) in previewTypeList" :key="index">
				<view class="swiper-item">
					<view class="preview">
						<image class="preview-img"
							:src="index === 0 ? userIdPhoto[selectedBgColor].img_wm_url_list : userIdPhoto[selectedBgColor].print_wm_url_list"
							mode="aspectFit" />
						<view class="nav-back" @click="navBack">
							<image src="@/static/img/arrow-back.png" mode="aspectFit"></image>
							<text>重拍</text>
						</view>
						<view class="tips">
							免费赠送电子证件照，打印后不会出现水印
						</view>
					</view>
				</view>
			</swiper-item>
		</swiper>
		<view class="preview-foot">
			<view class="swatches" @click="handleShowSelectBg(true)">
				<image src="@/static/img/swatches.png" mode="aspectFit"></image>
				<text>底色</text>
			</view>
			<view class="submit-btn" @click="navPayment">
				立即打印
			</view>
		</view>
		<u-popup :show="showSelectBg" mode="bottom" round="20rpx" @close="handleShowSelectBg(false)">
			<view class="select-bg-box">
				<view class="select-bg-title">
					请选择需要的背景颜色
				</view>
				<view class="select-bg-ctn">
					<view class="select-bg"
						:style="{background: `linear-gradient(to bottom, #${item.background_color.start_color.toString(16)}, #${item.background_color.enc_color.toString(16)})`}"
						v-for="(item, index) in userIdPhoto" :key="item.id" @click="changeBgColor(index)">
						<view class="color" :class="selectedBgColor === index ? 'active' : ''"></view>
					</view>
				</view>
				<view class="select-bg-foot">
					<view class="btn" @click="showSelectBg = false">
						取消
					</view>
					<view class="btn" @click="showSelectBg = false">
						确定
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 当前索引
				current: 0,
				previewTypeList: [{
						name: '单张照预览'
					},
					{
						name: '排版照预览'
					}
				],
				// 选中的背景颜色索引
				selectedBgColor: 0,
				showSelectBg: false,
			};
		},
		computed: {
			userIdPhoto() {
				return this.$store.state.idPhoto.userIdPhoto
			},
		},
		onLoad() {},
		methods: {
			changeCurrent(e) {
				this.current = e.index == undefined ? e.detail.current : e.index
			},
			/**
			 * 跳转支付
			 */
			navPayment() {
				this.$navTo('pages/indexs/photo_payment', {
					orderType: 1,
					selectIdPhotoIdx: this.selectedBgColor + 1
				})
			},
			/**
			 * 返回上一页
			 */
			navBack() {
				uni.navigateBack()
			},
			/**
			 * 展示更换底色面板
			 */
			handleShowSelectBg(flag) {
				this.showSelectBg = flag
			},
			/**
			 * 更换底色
			 */
			changeBgColor(index) {
				this.selectedBgColor = index
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		display: flex;
		flex-direction: column;
		min-width: 100vw;
		min-height: 100vh;
		background: #fff2e8;
	}

	.tab-list {
		border-top: 2rpx solid #f7f7f7;
		background-color: #fff;
	}

	.content {
		flex: 1;
		width: 100%;
		min-height: 90vh;
	}

	.swiper-item {
		padding: 40rpx;
	}

	.preview {
		display: flex;
		flex-direction: column;
		align-items: center;
		border-radius: 20rpx;
		background: #ffffff;
	}

	.preview-img {
		margin-top: 175rpx;
		width: 100%;
		height: 308rpx;
	}

	.nav-back {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 30rpx;
		width: 148rpx;
		height: 48rpx;
		border-radius: 10rpx;
		opacity: 1;
		border: 2rpx solid #e5e5e5;
		background: #f3f3f3;

		image {
			width: 20rpx;
			height: 20rpx;
		}

		text {
			margin-left: 4rpx;
			color: #a2a2a2;
			font-size: 20rpx;
		}
	}

	.tips {
		margin-top: 20rpx;
		margin-bottom: 174rpx;
		color: #c1c1c1;
		font-size: 20rpx;
	}

	.preview-foot {
		position: fixed;
		bottom: calc(60rpx + constant(safe-area-inset-bottom));
		bottom: calc(60rpx + env(safe-area-inset-bottom));
		left: 50%;
		transform: translateX(-50%);
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 60rpx;
		width: 668rpx;
		height: 214rpx;
		border-radius: 20rpx;
		background: #ffffff;
		z-index: 100;
	}

	.swatches {
		display: flex;
		flex-direction: column;
		align-items: center;

		image {
			width: 32rpx;
			height: 32rpx;
		}

		text {
			margin-top: 10rpx;
			color: #6d6d6d;
			font-size: 20rpx;
		}
	}

	.submit-btn {
		margin-left: 80rpx;
		width: 196rpx;
		height: 70rpx;
		line-height: 70rpx;
		border-radius: 10rpx;
		background: linear-gradient(135deg, #ffb300 0%, #ff8400 100%);
		color: #ffffff;
		text-align: center;
		font-size: 24rpx;
	}

	// 弹窗
	.select-bg-box {}

	.select-bg-title {
		margin-top: 70rpx;
		padding: 0 60rpx;
		font-size: 30rpx;
	}

	.select-bg-ctn {
		display: flex;
		flex-wrap: wrap;
		margin: 40rpx 0 0;
		padding: 0 60rpx;
	}

	.select-bg {
		position: relative;
		margin-right: 30rpx;
		margin-bottom: 40rpx;
		width: 40rpx;
		height: 40rpx;
		border-radius: 3rpx;

		.color {
			position: absolute;
			left: 50%;
			top: 50%;
			display: none;
			transform: translate(-50%, -50%);
			border-radius: 4rpx;
			width: 50rpx;
			height: 50rpx;
			border: 4rpx solid #ff8a07;
		}

		.active {
			display: block;
		}
	}

	.select-bg-foot {
		display: flex;
		justify-content: space-between;
		padding: 50rpx;
		border-top: 2rpx solid #eeeeee;

		.btn {
			font-size: 34rpx;
		}
	}
</style>